<!DOCTYPE html>

<!-- script标签会把请求过来的响应数据当做JS脚本自动执行 -->
<!-- 已登录，返回：var isLogin = true -->
<!-- 未登录，返回：var isLogin = false -->
<script src="/login/status"></script>
<script>
  // console.log(isLogin, 'isLogin');
  if (!isLogin) {
    location.href = '/admin/login.html'
  }
</script>

<html lang="zh-CN">

<head>
  <meta charset="utf-8">
  <title>Comments &laquo; Admin</title>
  <link rel="stylesheet" href="/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/assets/css/admin.css">
  <script src="/assets/vendors/nprogress/nprogress.js"></script>
</head>

<body>
  <script>NProgress.start()</script>

  <div class="main">
    <nav class="navbar">
      <button class="btn btn-default navbar-btn fa fa-bars"></button>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="profile.html"><i class="fa fa-user"></i>个人中心</a></li>
        <li><a href="login.html"><i class="fa fa-sign-out"></i>退出</a></li>
      </ul>
    </nav>
    <div class="container-fluid">
      <div class="page-title">
        <h1>所有评论</h1>
      </div>
      <div class="page-action">
        <ul class="pagination pagination-sm pull-right">
          <li><a href="#">上一页</a></li>
          <li><a href="#">1</a></li>
          <li><a href="#">2</a></li>
          <li><a href="#">3</a></li>
          <li><a href="#">下一页</a></li>
        </ul>
      </div>
      <table class="table table-striped table-bordered table-hover">
        <thead>
          <tr>
            <th>评论人</th>
            <th>评论内容</th>
            <th>评论在</th>
            <th>提交于</th>
            <th>状态</th>
            <th class="text-center" width="100">操作</th>
          </tr>
        </thead>
        <tbody id="listBox">


        </tbody>
      </table>
    </div>
  </div>

  <div class="aside">
    <div class="profile">
      <img class="avatar" src="/uploads/avatar.jpg">
      <h3 class="name">Coder</h3>
    </div>
    <ul class="nav">
      <li>
        <a href="index.html"><i class="fa fa-dashboard"></i>仪表盘</a>
      </li>
      <li>
        <a href="#menu-posts" class="collapsed" data-toggle="collapse">
          <i class="fa fa-thumb-tack"></i>文章<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-posts" class="collapse">
          <li><a href="posts.html">所有文章</a></li>
          <li><a href="post-add.html">写文章</a></li>
          <li><a href="categories.html">分类目录</a></li>
        </ul>
      </li>
      <li class="active">
        <a href="comments.html"><i class="fa fa-comments"></i>评论</a>
      </li>
      <li>
        <a href="users.html"><i class="fa fa-users"></i>用户</a>
      </li>
      <li>
        <a href="#menu-settings" class="collapsed" data-toggle="collapse">
          <i class="fa fa-cogs"></i>设置<i class="fa fa-angle-right"></i>
        </a>
        <ul id="menu-settings" class="collapse">
          <li><a href="slides.html">图片轮播</a></li>
          <li><a href="settings.html">网站设置</a></li>
        </ul>
      </li>
    </ul>
  </div>

  <script src="/assets/vendors/jquery/jquery.js"></script>
  <script src="/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script src="/assets/vendors/art-template/template-web.js"></script>
  <script>NProgress.done()</script>

  <script type="text/html" id="listTpl">
    {{ each data v }}
    <tr>
      <td>{{ v.author.nickName }}</td>
      <td>{{ v.content }}</td>
      <td>《{{ v.post.title }}》</td>
      <td>{{ v.createAt }}</td>
      <td>{{ ['未批准', '批准'][v.state] }}</td>
      <td class="text-center">
        <!-- 当前是未批准状态，显示批准操作 -->
        {{ if v.state === 0 }}
        <a href="javascript:;" class="btn btn-info btn-xs pizhunBtn" data-id="{{ v._id }}" data-state="1">批准</a>
        <!-- 当前是已批准状态，显示驳回操作 -->
        {{ else }}
        <a href="javascript:;" class="btn btn-warning btn-xs bohuiBtn" data-id="{{ v._id }}" data-state="0">驳回</a>
        {{ /if }}
        <a href="javascript:;" class="btn btn-danger btn-xs">删除</a>
      </td>
    </tr>
    {{ /each }}
  </script>

  <script>

    $(() => {

      const renderList = () => {
        $.ajax({
          type: 'GET',
          url: '/comments',
          success(res) {
            console.log(res);
            $('#listBox').html(template('listTpl', { data: res.records }))
          }
        })
      }

      renderList()

      $('#listBox').on('click', '.pizhunBtn,.bohuiBtn', ({ target }) => {
        $.ajax({
          type: 'PUT',
          url: `/comments/${target.dataset.id}`,
          data: { state: target.dataset.state },
          success() {
            renderList()
          }
        })
      })

    })

  </script>
</body>

</html>